#include <unistd.h>
#include <errno.h>

#include "MobsBuf.h"

#define MMAP_FILE_NAME "/tmp/buf01.mobs"

int main(int argc,char *argv[])
{
    int ret = 0,woid,roid;
    uintptr_t wuid,ruid;
    char className[64]=MMAP_FILE_NAME;

    if(argc > 1)
    {
	snprintf(className,sizeof(className),"%s",argv[1]);
    }
    mobs_unlink(className);

    wuid = mobs_open("producer",className,
	    MOBS_FLAG_CREATE|MOBS_FLAG_WRONLY|MOBS_FLAG_SHARED,
	    1024*1024,1024,4096);
    ruid = mobs_open("consumer",className,
	    MOBS_FLAG_CREATE|MOBS_FLAG_RDONLY|MOBS_FLAG_SHARED,
	    1024*1024,1024,4096);
    printf("open mobs wuid = 0x%08lx,ruid = 0x%08lx\n",wuid,ruid);

    if(wuid == -1 || ruid == -1)
	exit(EXIT_FAILURE);

    char data[1024]="hello mobs!";
    //test write
    ret = mobs_start(wuid,0);
    printf("write start ret = %d\n",ret);
    if(ret < 0) exit(EXIT_FAILURE);

    woid = ret;
    ret = mobs_write(wuid,woid,data,strlen(data));
    printf("write data:%s ret = %d\n",data,ret);
    if(ret < 0)
    {
	exit(EXIT_FAILURE);
    }
    mobs_finish(wuid,woid);
    
    //test read
    ret = mobs_start(ruid,0);
    printf("read start ret = %d\n",ret);
    if(ret < 0) exit(EXIT_FAILURE);

    roid = ret;
    ret = mobs_read(ruid,roid,data,sizeof(data));
    printf("read data:%s ret = %d\n",data,ret);
    if(ret < 0)
    {
	exit(EXIT_FAILURE);
    }
    mobs_finish(ruid,roid);

    mobs_close(wuid);
    printf("close mobs wuid = 0x%08lx\n",wuid);
    mobs_close(ruid);
    printf("close mobs ruid = 0x%08lx\n",ruid);

    return 0;
}

